// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// w [author] rhyme_lph
// A [email]  rhymelph@gmail.com
// I [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/widgets/button/ace_close_button.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/bar_code_camera_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:r_barcode/r_barcode.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "barCodeCameraPage")
class BarCodeCameraPage extends StatefulWidget {
  @override
  _BarCodeCameraPageState createState() => _BarCodeCameraPageState();
}

class _BarCodeCameraPageState extends State<BarCodeCameraPage>
    with
        PagePresenter<BarCodeCameraPage, BarCodeCameraProvider>,
        WidgetsBindingObserver {
  @override
  Widget get buildWidget => Scaffold(
        backgroundColor: Colors.black,
        body: Stack(
          children: [
            Center(
              child: MaterialBarCodeFrameWidget(
                scanStatus: BarcodeScanStatus.scan,
                scanType: BarcodeScanType.barCode,
                child: listBuilder<Tuple2<double, bool>>(
                  selector: (_, __) => Tuple2(__.controller?.value?.aspectRatio,
                      __.controller?.value?.isInitialized),
                  builder: (BuildContext context, Tuple2<double, bool> value,
                          Widget child) =>
                      AspectRatio(
                    aspectRatio: value.item1 ?? 1,
                    child: GestureDetector(
                        onTapDown: (detail) {
                          provider.controller?.requestFocus(
                              detail.globalPosition.dx /
                                  MediaQuery.of(context).size.width,
                              detail.globalPosition.dy /
                                  MediaQuery.of(context).size.height,
                              100,
                              100);
                        },
                        child: value.item1 == null
                            ? Container()
                            : RBarcodeCamera(provider.controller)),
                  ),
                ),
              ),
            ),
            AceCloseButton(),
          ],
        ),
      );

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      provider.controller?.startScan();
    } else if (state == AppLifecycleState.paused) {
      provider.controller?.stopScan();
    }
  }
}
